import { observable, action } from 'mobx';
import * as Apis from '../../apis/Api';

export default class ApiSingleShowStore {
    @observable apiId = '';// 接口id
    @observable showTemplet;// 显示模板
    @observable isSuccess = false;
    @observable loading = false;
    @observable showAlertIcon = false;// 是否显示提示alert页
    @observable name = '';
    @observable islist = '0';
    @observable showSpin = true;// 是否显示加载图标
    @observable nameLink = '1';// 根据企业名称获取模糊匹配链接地址
    // 处理参数
    @action
    initSingApiPage = (props) => {
      // console.log(props)
      if (props.location) { // 通过链接
        this.front_path = `/${props.params.frontPath}`;
        this.singRequestParam = props.location.query;
      } else { // 通过组件
        this.singRequestParam = props.requestParam;
        this.front_path = props.frontPath;
      }
      if (this.front_path != '/undefined') {
        this.getIdByFrontPath(this.front_path);
      }
      if (props.showAlertIcon) {
        this.showAlertIcon = props.showAlertIcon;
      }
      if (props.showAlertIcon) {
        this.name = props.showAlertIcon;
      }
      if (props.islist) {
        this.islist = props.islist;
      }
      if (typeof (props.showSpin) !== 'undefined') {
        this.showSpin = props.showSpin;
      }
      if (props.nameLink) {
        this.nameLink = props.nameLink;
      }
    }

    @action
    getIdByFrontPath = (frontPath) => {
      this.loading = true;
      this.isSuccess = false;
      Apis.getIdByFrontPath({ frontPath }).then((data) => {
        if (data.status == 'success') {
          this.apiId = data.apiId;
          this.showTemplet = data.showTemplet;
          const objToStr = JSON.stringify(this.singRequestParam);
          this.singRequestParam = objToStr;
          this.name = data.apiName;
          this.islist = data.islist;
          // console.log(objToStr)
          this.isSuccess = true;
        } else {
          this.msgcode = data.msgcode;
          this.isSuccess = false;
        }
        this.loading = false;
      });
    }
}
